// config-plugins.js 自定义插件
const { withAndroidManifest, withDangerousMod } = require('@expo/config-plugins');

// 自动生成wxapi目录结构
const withWechatAndroidFiles = config => {
    return withDangerousMod(config, [
        'android',
        async config => {
            const packagePath = config.android.package.replace(/\./g, '/');
            const wxapiDir = `android/app/src/main/java/${packagePath}/wxapi`;

            await require('fs-extra').ensureDir(wxapiDir);
            await require('fs-extra').writeFile(
                `${wxapiDir}/WXEntryActivity.java`,
                `package ${config.android.package}.wxapi;

import expo.modules.wechat.WeChatCallbackActivity;
import android.os.Bundle;

public class WXEntryActivity extends WeChatCallbackActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }
}`
            );
            return config;
        }
    ]);
};

// 修改AndroidManifest
const withCustomAndroidManifest = config => {
    return withAndroidManifest(config, config => {
        const app = config.modResults.manifest.application[0];

        app.activity = app.activity || [];
        app.activity.push({
            $: {
                "android:name": ".wxapi.WXEntryActivity",
                "android:exported": "true",
                "android:launchMode": "singleTask",
                "android:taskAffinity": config.android.package,
                "android:theme": "@android:style/Theme.Translucent.NoTitleBar"
            }
        });

        return config;
    });
};

module.exports = config => {
    config = withWechatAndroidFiles(config);
    config = withCustomAndroidManifest(config);
    return config;
};
